#include "IndexBufferMaterial.h"

#include "Material.h"
#include "ShaderMaterial.h"
#include "IndexedGeometry.h"

IndexBufferMaterial::IndexBufferMaterial(IndexedGeometry* ig)
:m_pIndexBuffer(ig),m_pMaterial(NULL),m_pShaderMaterial(NULL),m_MaterialSet(false)
{

}


IndexBufferMaterial::IndexBufferMaterial(IndexedGeometry* ig,Material* material)
:m_pIndexBuffer(ig),m_pMaterial(material),m_pShaderMaterial(NULL)
{

}

IndexBufferMaterial::~IndexBufferMaterial(void)
{
	SAFE_DELETE(m_pIndexBuffer);
	if ( m_pMaterial != NULL )
		SAFE_DELETE(m_pMaterial);
	// do not delete m_pShaderMaterial, it is deleted
	// by the EffectLibrary
}

void IndexBufferMaterial::SetShaderMaterial(ShaderMaterial* material)
{
	m_pShaderMaterial = material;
	if ( m_pMaterial != NULL )
	{
		if ( m_pMaterial->IsDiffuseTextureSet() ){
			FXParameter* param = m_pShaderMaterial->CustomizeParameterByName(_T("gDiffuseMap"));
			if ( param != NULL )
				param->SetValue(m_pMaterial->GetDiffuseDXTexture());
		}
		if ( m_pMaterial->IsNormalMapTextureSet()){
			FXParameter* param = m_pShaderMaterial->CustomizeParameterByName(_T("gNormalMap"));
			if ( param != NULL)
				param->SetValue(m_pMaterial->GetNormalMapDXTexture());
		}
		if ( m_pMaterial->IsSpecularTextureSet()){
			FXParameter* param =m_pShaderMaterial->CustomizeParameterByName(_T("gSpecularMap"));
			if ( param != NULL)
				param->SetValue(m_pMaterial->GetSpecularDXTexture());
		}
		FXParameter* param =  m_pShaderMaterial->CustomizeParameterByName(_T("gShininess"));
		param->SetValue(m_pMaterial->GetShininessAsParameter());
	}else if ( material != NULL)
		m_MaterialSet=true;
}